import that from '../main'
// 设置localStorage
function setStorage(name, content) {
  if (!name) return
  if (typeof content !== 'string') {
    content = JSON.stringify(content)
  }
  window.localStorage.setItem(name, content)
}

// 获取localStorage
function getStorage(name) {
  if (!name) return
  return window.localStorage.getItem(name)
}

// 删除localStorage
function removeStorage(name) {
  if (!name) return
  window.localStorage.removeItem(name)
}

// 清楚所有localStorage
function clearStorage() {
  window.localStorage.clear()
}

// 获取地址栏参数
function getUrl(name) {
  let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
  let subIndex = window.location.href.indexOf('?') + 1
  let r = window.location.href.substr(subIndex).match(reg)
  if (r != null) {
    return decodeURI(r[2])
  } else {
    return null
  }
}

// 加载等待
function loading(msg) {
  // Toast.loading({
  //   mask: true,
  //   message: msg,
  //   duration: 0
  // })
}

// 清除加载
function loadingClear() {
  // Toast.clear()
}

// 提示信息
function toast(data) {
  return new Promise(resolve => {
    that.$message({
      showClose: true,
      message: data.msg,
      type: 'warning',
      duration: data.duration || 1500,
      onClose: function() {
        resolve()
      }
    })
  })
}

// 提示框
function alert(msg) {
  return new Promise(resolve => {
    that.$alert(msg, '提示', {
      confirmButtonText: '确定',
      callback: () => {
        resolve()
      }
    })
  })
}

// 确认框
function confirm(data) {
  return new Promise((resolve, reject) => {
    that
      .$confirm(data.msg, data.title || '提示', {
        confirmButtonText: data.confirmBtn || '确认',
        cancelButtonText: data.cancelBtn || '取消',
        type: 'info'
      })
      .then(() => {
        resolve()
      })
      .catch(() => {
        reject()
      })
  })
}

// 获取地址栏的token 存入缓存
function getUrlToken() {
  let token = getUrl('user_token')
  if (token) {
    setStorage('token', token)
  }
}

function getUrlPath(str) {
  const index = str.indexOf('#')
  str = str.substring(index + 1, str.length)
  return str
}

/**
 * 数组去重
 */
function ArrayUnique(arr) {
  let x = new Set(arr)
  return [...x]
}

/**
 * 根据键值排序
 * @param {*} 键值
 */
function compare(key) {
  return (obj1, obj2) => {
    if (Number(obj1[key]) < Number(obj2[key])) {
      return -1
    } else if (obj1[key] === obj2[key]) {
      return 0
    } else {
      return 1
    }
  }
}

function compareDesc(key) {
  return (obj1, obj2) => {
    return obj2[key] - obj1[key]
  }
}

function handleTime(time, type) {
  if (time === 0) {
    return ' '
  }
  let date = new Date(parseInt(time) * 1000)
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? '0' + m : m
  let d = date.getDate()
  d = d < 10 ? '0' + d : d
  let h = date.getHours()
  let minute = date.getMinutes()
  h = h < 10 ? '0' + h : h
  minute = minute < 10 ? '0' + minute : minute
  if (type == 'date') {
    return y + '-' + m + '-' + d
  } else if (type == 'time') {
    return h + ':' + minute
  } else {
    return y + '-' + m + '-' + d + ' ' + h + ':' + minute
  }
}

export default {
  setStorage: setStorage,
  getStorage: getStorage,
  removeStorage: removeStorage,
  clearStorage,
  getUrl: getUrl,
  getUrlToken: getUrlToken,
  loading: loading,
  loadingClear: loadingClear,
  toast: toast,
  getUrlPath: getUrlPath,
  alert: alert,
  confirm: confirm,
  ArrayUnique,
  compare,
  compareDesc,
  handleTime
}
